{
Traviesadora beta 0.2.0.13.33 (30.11.2008)
LastMod: 11.12.2008 by Prof1983

Copyright 2008, travieorama@googlemail.com

This file is part of the taviesadora project

taviesadora is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

taviesadora is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
}
unit WinLib;

interface

uses
  Forms, Classes, Windows;

type
  TWinItem =  record
    Win: TForm;
    singlewatcher: Boolean;
  end;
  PWinItem = ^TWinItem;
  TWinLib = class
  public
    constructor Create;
    destructor Destroy;   override;
    function QueryEnd:boolean;
    procedure clear;
    procedure RegisterWin(frm:TForm);
    procedure SetSingleWatcher(frm:TForm;SingleWatcher:boolean);
    procedure UnRegisterWin(frm:TForm);
    function GetFirstWin(singlewatcher:boolean):TForm;
  private
    WinList:TList;
    closing:boolean;
    unregistered:boolean;
    function SearchWin(Frm:TForm):integer;
    procedure Del(i:integer);
  end;
implementation

uses
  {Movie,} Unit1;

{ TMovieWinLib }

constructor TWinLib.create;
begin
  inherited create;
  WinList := TList.create;
  closing := false;
end;

procedure TWinLib.Del(i: integer);
var
  wi:PWinItem;
begin
  if (i > -1) and (i<WinList.Count) then begin
    wi := WinList[i];
    dispose(wi);
    WinList.delete(i);
  end;
end;

function TWinLib.QueryEnd: boolean;
var
  i:integer;
  wi:PWinItem;
begin
  result := WinList.Count=0;
  if closing then
    result := false;
 // SendMessage(Application.MainForm.Handle, WM_CLOSEAPP, 0, 0);
end;

procedure TWinLib.clear;
var
  i:integer;
  wi:PWinItem;
begin
  if closing then exit;
  closing := true;
  while WinList.Count > 0 do
  begin
    wi := WinList[0];
    if wi.Win is TfrmMovie then
    begin
      i := WinList.Count;
      unregistered := false;
      (wi.Win as TfrmMovie).close;
      while not unregistered do begin
        Application.ProcessMessages;
      end;
    end else
      Del(0);
  end;
  closing := false;
  SendMessage(Application.MainForm.Handle, WM_CLOSEAPP, 0, 0)
end;

destructor TWinLib.destroy;
begin
  WinList.Free;
  inherited destroy;
end;

function TWinLib.GetFirstWin(singlewatcher:boolean): TForm;
var
  wi:PWinItem;
  i:integer;
begin
  result := nil;
  if WinList.count > 0 then begin
    for i := 0 to winList.count-1 do begin
      wi := Winlist[0];
      if wi.singlewatcher = singlewatcher then begin
        result := wi.Win;
      end;
    end;
  end else
    result := nil;
end;



procedure TWinLib.RegisterWin(frm: TForm);
var
  i:integer;
  wi:PWinItem;
begin
  i := SearchWin(frm);
  if i = -1 then begin
    new(wi);
    wi.Win := frm;
    WinList.Add(wi);
  end;
end;

function TWinLib.SearchWin(Frm: TForm): integer;
var
  i:integer;
  wi:PWinItem;
begin
  result := -1;
  for i := 0 to WinList.Count-1 do begin
    wi := WinList[i];
    if wi.Win = Frm then begin
      result := i;
      break;
    end;
  end;
end;

procedure TWinLib.SetSingleWatcher(frm: TForm; SingleWatcher: boolean);
var
  i:integer;
  wi:PWinItem;
begin
  i := SearchWin(frm);
  if i > -1 then begin
    wi := WinList[i];
    wi.singlewatcher := SingleWatcher;
  end;
end;


procedure TWinLib.UnRegisterWin(frm: TForm);
var
  i:integer;
  wi:PWinItem;
begin
  unregistered := true;
  i := SearchWin(frm);
  if i > -1 then begin
    Del(i);
  end;
end;



end.
